using System;
using Microsoft.Build.Framework;

namespace QuickTest.Core.Compilation
{
    public class CompilerLogger : ILogger
    {
        private readonly ICompilerEvents compilerEvents;

        public CompilerLogger(ICompilerEvents compilerEvents)
        {
            this.compilerEvents = compilerEvents;
        }

        public void Initialize(IEventSource eventSource)
        {
            eventSource.BuildStarted += eventSource_BuildStarted;
            eventSource.BuildFinished += eventSource_BuildFinished;
            eventSource.ErrorRaised += eventSource_ErrorRaised;
        }

        private void eventSource_BuildStarted(object sender, BuildStartedEventArgs e)
        {
            compilerEvents.RaiseCompileStarted(new CompileStartedEventArgs(e.Message));
        }

        private void eventSource_BuildFinished(object sender, BuildFinishedEventArgs e)
        {
            compilerEvents.RaiseCompileFinished(new CompileFinishedEventArgs(e.Message, e.Succeeded));
        }

        private void eventSource_ErrorRaised(object sender, BuildErrorEventArgs e)
        {
            compilerEvents.RaiseCompileError(
                new CompileErrorEventArgs(String.Format("{0}({1},{2}): error {3}: {4}", e.File, e.LineNumber, e.ColumnNumber, e.Code, e.Message)));
        }

        public void Shutdown()
        {
        }

        public LoggerVerbosity Verbosity
        {
            get; set;
        }

        public string Parameters
        {
            get; set;
        }
    }
}